﻿// **************************************************
// 以下代码由代码生成器生成。
// 请不要修改该文件，以避免您的修改被代码生成器覆盖。
// **************************************************
using System.Linq;
using Volo.Abp.Localization;
using Volo.Abp.Authorization.Permissions;
using Joy.Erp.BasicArchives.Localization;

namespace {{namespace_prefix}}.Permissions
{
    public class {{class_name}}PermissionDefinitionPovider : PermissionDefinitionProvider
    {
        public override void Define(IPermissionDefinitionContext context)
        {
            var menus = context.GetGroupOrNull({{project_group_name}}Permissions.Menus);
            if (menus == null)
            {
                menus = context.AddGroup({{project_group_name}}Permissions.Menus, L("Permission:Menus"));
            }

            var basicArchivesMenu = menus.GetPermissionOrNull({{project_group_name}}Permissions.{{project_group_name}}Menu.Default);
            if (basicArchivesMenu == null)
            {
                basicArchivesMenu = menus.AddPermission({{project_group_name}}Permissions.{{project_group_name}}Menu.Default, L("Permission:Menus.{{project_group_name}}"));
            }

            var parentMenu = basicArchivesMenu.Children.FirstOrDefault(x => x.Name == {{project_group_name}}Permissions.{{class_name}}.ParentMenu);
            if (parentMenu == null)
            {
                parentMenu = basicArchivesMenu.AddChild({{project_group_name}}Permissions.{{class_name}}.ParentMenu, L("Permission:Menus.{{project_group_name}}.{{parent_menu}}"));
            }

            var menu = parentMenu.Children.FirstOrDefault(x => x.Name == {{project_group_name}}Permissions.{{class_name}}.Menu); 
            if (menu == null)
            {
                menu = parentMenu.AddChild({{project_group_name}}Permissions.{{class_name}}.Menu, L("Permission:Menus.{{project_group_name}}.{{parent_menu}}.{{class_name}}"));
                menu.RequirePermissions({{project_group_name}}Permissions.{{class_name}}.Query);
            }

            var group = context.GetGroupOrNull({{project_group_name}}Permissions.GroupName);
            if (group == null)
            {
                group = context.AddGroup({{project_group_name}}Permissions.GroupName, L("Permission:{{project_group_name}}"));
            }

            var p{{class_name}}Default = group.AddPermission({{project_group_name}}Permissions.{{class_name}}.Default, L("Permission:{{project_group_name}}.{{parent_menu}}.{{class_name}}"));
            var p{{class_name}}Query = p{{class_name}}Default.AddChild({{project_group_name}}Permissions.{{class_name}}.Query, L("Permission:Query"));
            var p{{class_name}}Edit = p{{class_name}}Default.AddChild({{project_group_name}}Permissions.{{class_name}}.Edit, L("Permission:Edit"));
            var pCreate = p{{class_name}}Edit.AddChild({{project_group_name}}Permissions.{{class_name}}.Create, L("Permission:Create"));
            var pUpdate = p{{class_name}}Edit.AddChild({{project_group_name}}Permissions.{{class_name}}.Update, L("Permission:Update"));
            var pDelete = p{{class_name}}Edit.AddChild({{project_group_name}}Permissions.{{class_name}}.Delete, L("Permission:Delete"));
        }

        private static LocalizableString L(string name)
        {
            return LocalizableString.Create<{{project_group_name}}Resource>(name);
        }
    }
}

